package oh.june.imagestogif;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

	private LayoutInflater mInflater;
	private LinearLayout mAttachBase;
	private int GALLERY_REQUEST = 10002;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button btnAdd = (Button)findViewById(R.id.btn_add);
		btnAdd.setOnClickListener(this);
		
		Button btnConvert = (Button)findViewById(R.id.btn_convert);
		btnConvert.setOnClickListener(this);
		
		mAttachBase = (LinearLayout)findViewById(R.id.image_attach_base);
		mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch(id){
			case R.id.btn_add:
				add();
				break;
			case R.id.btn_convert:
				moveToConvertGifActivity();
				break;
		}
	}

	private void moveToConvertGifActivity() {
		if(mAttachBase.getChildCount() == 0)
			return;
		
		ArrayList<String> picturePathList = new ArrayList<String>();
		for(int i=0; i<mAttachBase.getChildCount(); i++){
			LinearLayout child = (LinearLayout) mAttachBase.getChildAt(i);
			for(int j=0; j<child.getChildCount(); j++){
				ImageView view = (ImageView)child.getChildAt(j);
				if(view.getTag() == null)
					continue;
				picturePathList.add(view.getTag().toString());
			}
		}
		
		Intent i = new Intent(MainActivity.this, MakeGifActivity.class);
		i.putExtra("PICTURE_PATH_LIST", picturePathList);
		startActivity(i);
		
	}

	private void add() {
		if(canAttachInCurrentRow()){
			takePicFromGallery();
		}else{
			addNewRow();
		}
	}
	
	private boolean canAttachInCurrentRow() {
		boolean canAttach = false;
		if(mAttachBase.getChildCount() == 0)
			return canAttach;
		
		LinearLayout lastChild = (LinearLayout) mAttachBase.getChildAt(mAttachBase.getChildCount()-1);
		for(int i=0; i<lastChild.getChildCount(); i++){
			ImageView view = (ImageView)lastChild.getChildAt(i);
			if(view.getTag() != null)
				continue;
			canAttach = true;
		}
		return canAttach;
	}
	
	private void attachImageCurrentRow() {
		
	}
	
	@SuppressLint("InflateParams")
	private void addNewRow() {
		View v = mInflater.inflate(R.layout.control_image_thumnail, null);
		mAttachBase.addView(v);
		mAttachBase.invalidate();
	}
	
	protected void takePicFromGallery() {
		Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(i, GALLERY_REQUEST);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();
			
			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			
			addImageToImageView(picturePath);
			
		}

	}
	
	private void addImageToImageView(String picturePath) {
		ImageView imv = getLastImageView();
		if(imv == null)
			return;
			
		imv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
		imv.setTag(picturePath);
	}
	
	private ImageView getLastImageView(){
		ImageView imageView = null;
		LinearLayout lastChild = (LinearLayout) mAttachBase.getChildAt(mAttachBase.getChildCount()-1);
		for(int i=0; i<lastChild.getChildCount(); i++){
			ImageView view = (ImageView)lastChild.getChildAt(i);
			if(view.getTag() != null)
				continue;
			imageView = view;
		}
		return imageView;
	}

}
